<?php
namespace app\api\controller\wanlshop;

use addons\wanlshop\library\WanlPay\WanlPay;
use addons\wanlshop\library\WanlSdk\Common;
use app\api\model\wanlshop\PayOutTrade;
use app\common\controller\Api;
use app\index\library\wechat\MiniApp;
use fast\Http;
use think\Cache;
use think\Db;
use think\Exception;


/**
 * WanlShop支付接口
 */
class Face extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //h5火脸支付
    public function faceH5(){
        $order_ids = $this->request->post('order_id/a');
        if (!$order_ids ){
            $this->error('参数错误');
        }
        // 判断权限
        $model_order = model('app\api\model\wanlshop\Order');
        $model_order_goods = model('app\api\model\wanlshop\OrderGoods');
        $model_goods = model('app\api\model\wanlshop\Goods');
        $model_goods_sku = model('app\api\model\wanlshop\GoodsSku');
        $order = $model_order
            ->where('id', 'in', $order_ids)
            ->select();
        if(!$order){
            $this->error(__('没有找到任何要支付的订单'));
        }
        foreach($order as $item){
            if($item['state'] != 1){
                $this->error(__('订单已支付，或网络繁忙'));
            }
            // 1.0.5升级 修复付款减库存
            foreach($model_order_goods->where('order_id', $item['id'])->select() as $data){
                $redis = Common::redis();
                // 获取sku
                $sku = $model_goods_sku->get($data['goods_sku_id']);
                // 1.1.2升级
                $sku_key = 'goods'.'_'.$sku['goods_id'].'_'.$sku['id'];
                // 查询商品
                $goods = $model_goods
                    ->where(['id' => $data['goods_id'], 'stock' => 'payment'])
                    ->find();
                // 库存计算方式:porder=下单减库存,payment=付款减库存 1.0.8升级
                if($goods) {
                    // 1.1.2升级
                    if($data['number'] > $redis->llen("{$sku_key}")){
                        $this->error("系统繁忙，请稍后抢购！");
                    }else{
                        for ($i = 0; $i < $data['number']; $i ++) {
                            $redis->rpop("{$sku_key}");
                        }
                        $sku->setDec('stock', $data['number']); // 1.0.3升级
                    }
                }
            }
        }

        // 添加第三方支付订单信息
        // 获取支付信息
        $pay = model('app\api\model\wanlshop\Pay')
            ->where('order_id', 'in', $order_ids)
            ->where(['type' => 'goods'])
            ->select();
        // 1.0.8 升级
        $price = 0; // 付款金额
        $order_no = []; // 订单号
        $pay_id = []; // 交易号
        foreach($pay as $row){
            $price = bcadd($price, $row['price'], 2); // 总价格
//            $order_no[] = $row['order_no']; // 订单集
            $pay_id[] = $row['id']; // 交易集
        }
        $payOutTrade = (new PayOutTrade());
        $payOutTrade->out_trade_no = date('YmdHis'). rand(10000000,99999999). substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        $payOutTrade->pay_id = $pay_id;
        $payOutTrade->order_id = $order_ids;
        $payOutTrade->price = $price;
        $payOutTrade->type = 1;
        if($payOutTrade->save()){
            // 发起支付
            $result = (new \app\api\controller\Pay())->preApplet(['order_no'=>$payOutTrade->out_trade_no,'price'=>$price]);
            $this->success('',$result);
        }else{
            $this->error('支付失败');
        }
    }
}